package com.kingwang.training.ooad.coffeemaker.goodway.impl;

import com.kingwang.training.ooad.coffeemaker.goodway.HotWaterSource;
import com.kingwang.training.ooad.coffeemaker.goodway.Pollable;
import com.kingwang.training.ooad.coffeemaker.hardware.api.BoilerState;
import com.kingwang.training.ooad.coffeemaker.hardware.api.BoilerStatus;
import com.kingwang.training.ooad.coffeemaker.hardware.api.CoffeeMakerApi;
import com.kingwang.training.ooad.coffeemaker.hardware.api.ReliefValveState;

/**
 * @author King
 */
public class M4HotWaterSource extends HotWaterSource implements Pollable {
    private CoffeeMakerApi coffeeMakerApi;

    public M4HotWaterSource(CoffeeMakerApi coffeeMakerApi) {
        this.coffeeMakerApi = coffeeMakerApi;
    }

    @Override
    public Boolean isReady() {
        BoilerStatus status = this.coffeeMakerApi.getBoilerStatus();

        return BoilerStatus.NOT_EMPTY.equals(status);
    }

    @Override
    public void startBrewing() {
        this.coffeeMakerApi.setReliefValveState(ReliefValveState.CLOSED);
        this.coffeeMakerApi.setBoilerState(BoilerState.ON);
    }

    @Override
    public void pause() {
        this.coffeeMakerApi.setBoilerState(BoilerState.OFF);
        this.coffeeMakerApi.setReliefValveState(ReliefValveState.OPEN);
    }

    @Override
    public void resume() {
        startBrewing();
    }

    @Override
    public void poll() {
        BoilerStatus status = this.coffeeMakerApi.getBoilerStatus();

        if (this.brewing) {
            if (BoilerStatus.EMPTY.equals(status)) {
                pause();
                declareDone();
            }
        }
    }
}
